﻿using LyData.Data;
using System;
using System.Data;
using System.Web;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_PicList : System.Web.UI.Page
{
    protected void Bind()
    {
        string str = (string)this.Session["MenuId"];
        string text1 = (string)this.Session["FileName"];
        string str2 = (string)this.Session["MenuName"];
        if (str == null)
        {
            ManagerLogin.Out();
        }
        else
        {
            string str3;
            this.PicLink.NavigateUrl = "Pic.aspx?menuId=" + str;
            this.MenuName.Text = str2;
            IDb db = new DbFactory().DriverDb();

            db.Open();
            DataTable dataTable = db.DataTable("OrderId,ClassName,FileName,ParentId,PicClassId", "PicClass", string.Format("MenuId={0}", str), "OrderId ASC");
            if (this.IsSearch.Value == "1")
            {
                string parentId = this.ClassList.SelectedValue.ToString();
                string str5 = Function.ChildIdList(dataTable, "PicClassId", "ParentId", parentId, true);
                str3 = string.Format("MenuId={0} AND PicClassId IN({1})", str, str5);
                string text = this.PicTitle.Text;
                if (!((text == "") | (text == null)))
                {
                    str3 = string.Format("{0} AND (Title LIKE '%{1}%' OR PicContent LIKE '%{1}%')", str3, text);
                }
            }
            else
            {
                str3 = string.Format("MenuId={0}", str);
            }
            DataTable table2 = db.DataTable("ClassName,PicClassId,PicId,Title,PicPathSmall,PicPathBig,AddTime,Hot,Groom", "PicList", str3, "Weight Asc,PicClassId ASC,OrderId ASC,AddTime DESC");
            db.Close();
            this.PicList.DataSource = table2;
            this.PicList.DataBind();
            dataTable.Columns.Add("ClassNameText");
            DataTable newTable = dataTable.Clone();
            Function.PopulateTable(dataTable, newTable, "ClassName", "PicClassId", "ParentId", 0, 0);
            if (!base.IsPostBack)
            {
                this.ClassList.DataSource = newTable;
                this.ClassList.DataTextField = "ClassNameText";
                this.ClassList.DataValueField = "PicClassId";
                this.ClassList.DataBind();
            }
        }
    }

    protected void ProductsLock_CheckedChanged(object sender, EventArgs e)//热点
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        CheckBox ckeSelect = null;
        for (int i = 0; i < PicList.Rows.Count; i++)
        {
            ckeSelect = (CheckBox)PicList.Rows[i].FindControl("ProductsLock");
            string str = (ckeSelect.Checked == true ? "True" : "False");
            string strwhere = string.Format("PicId ={0}", PicList.Rows[i].Cells[0].Text);
            string[,] includeColumns = new string[,] { { "Hot", str, "bit", "0" } };
            db.Update("pic", includeColumns, strwhere);
        }
        db.Close();
        this.Bind();
    }

    protected void ProductsLocks_CheckedChanged(object sender, EventArgs e)
    {
        IDb db = new DbFactory().DriverDb();
        db.Open();
        CheckBox ckeSelect = null;
        for (int i = 0; i < PicList.Rows.Count; i++)
        {
            ckeSelect = (CheckBox)PicList.Rows[i].FindControl("ProductsLocks");
            string str = (ckeSelect.Checked == true ? "True" : "False");
            string strwhere = string.Format("PicId ={0}", PicList.Rows[i].Cells[0].Text);
            string[,] includeColumns = new string[,] { { "Groom", str, "bit", "0" } };
            db.Update("pic", includeColumns, strwhere);
        }
        db.Close();
        this.Bind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Msg.Visible = false;
        if (!base.IsPostBack)
        {
            this.Bind();
        }
    }

    protected void PicList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.PicList.PageIndex = e.NewPageIndex;
        this.Bind();
    }

    protected void PicList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    }

    protected void PicList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int num = e.Row.DataItemIndex - (this.PicList.PageIndex * this.PicList.PageSize);
        if (num >= 0)
        {
            string str = this.PicList.DataKeys[num].Values[1].ToString();
            Button button = (Button)e.Row.FindControl("DelBt");
            HyperLink link = (HyperLink)e.Row.FindControl("EditHyperLink");
            if (button != null)
            {
                if (!ManagerLogin.ValidateRole(string.Format("2_{0}_b", str)))
                {
                    button.Enabled = false;
                }
                if (!ManagerLogin.ValidateRole(string.Format("2_{0}_a", str)))
                {
                    link.Enabled = false;
                }
            }
        }
    }

    protected void PicList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int rowIndex = e.RowIndex;
        string str = this.PicList.DataKeys[rowIndex].Values[0].ToString();
        IDb db = new DbFactory().DriverDb();
        db.Open();
        db.Delete("Pic", string.Format("PicId={0}", str));
        db.Close();
        this.Bind();
        this.Msg.Text = "图文删除成功！";
        this.Msg.Visible = true;
    }

    protected void PicList_RowEditing(object sender, GridViewEditEventArgs e)
    {
    }

    protected void PicList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }

    protected void Search_Click(object sender, EventArgs e)
    {
        this.IsSearch.Value = "1";
        this.Bind();
    }
}
